﻿(function (jq) {
    jq.extend({
        //bnner滚动
        PowerP: function (id, num, time) {

            var prop = {
                id: id,
                num: num,
                class: '.cms-powerp-item',
                time: time,
                runtime: 3000,
                divs: null,
                currnum: 0,
                isRun: false,
                stop: false,
                init: function () {

                    if (this.num < 2) return;

                    this.divs = jq(id);
                    console.log(this.divs)
                    var self = this;

                    this.divs.find(this.class).each(function (index, item) {
                        if (index > 0) {
                            jq(item).css({ "left": "100%" });
                        }
                    });
                    setTimeout(function () {
                        self.runToNext();
                    }, self.time);
                },
                runToNext: function () {
                    var self = this;
                    var curr = this.currnum;
                    var next = curr + 1;
                    if (next >= this.num) next = 0;
                    this.currnum = next;
                    console.log(next)
                    this.divs.find(this.class + ":eq(" + next + ")").css({ "left": "100%" });
                    this.divs.find(this.class + ":eq(" + curr + ")").animate({ left: "-100%" }, this.runtime);
                    this.divs.find(this.class + ":eq(" + next + ")").animate({ left: "0" }, this.runtime);

                    setTimeout(function () {
                        self.runToNext();
                    }, self.time + self.runtime);
                }
            };
            prop.init();
            return prop;
        },
        ajaxPath:"/CMS/AjaxPage",
        ajaxLabel: function (param, func) {
            $.get(this.ajaxPath, param, function (e) {
                func(e);
            })
        },
        //隐藏
        hideNextPage: function () {
            $("#nextpage_btn").hide();
        },
        //显示
        showNextPage: function () {
            $("#nextpage_btn").show();
        },
        //上拉事件
        addNextPageEvent: function (func) {
            var isNext = false;//是否可触发事件
            var dw = $("#nextpage_btn");
            $(window).off("scroll");
            $(window).on("scroll", function () {
                if (isNext) {
                    return;
                }
                var dh = $(document).height();//文档高度
                var dt = $(document).scrollTop();//滚动高度
                var wh = $(window).height();//窗口高度
             //   console.log(dh + ":" + dt + ":" + wh);
                if (dh == dt + wh) {
                    isNext = true;
                    if (!dw.is("div")) {
                        dw = $("<div id='nextpage_btn'></div>");
                        dw.appendTo("body");
                    }
                    dw.html("加载中...");
                    func(function (none) {
                        isNext = false;
                        if (none) {
                            dw.html("没有更多了!");
                            $(window).off("scroll");
                        }
                        else { dw.height("加载更多!"); }
                    });
                }
            });

        }
        ,
        //点击数及相关记录
        addToClick: function (nid,cid) {    
            //是否新用户
            var userid = localStorage.getItem("userid");
            var sessionid = sessionStorage.getItem("sessionid");
            var newuser = false;
            var newsession = false;
            if (!userid) {
                newuser = true;
                userid = this.uuid();
                localStorage.setItem("userid",userid)

            }
            if (!sessionid) {
                newsession = true;
                sessionid = this.uuid();
                sessionStorage.setItem("sessionid",sessionid)

            }
            $.post("/CMS/AddHit", { Cid: cid, Nid: nid, UserId: userid, SessionId: sessionid, IsNewUser: newuser, IsNewSession: newsession, PageUrl: location.href },
                function (e) {
                    console.log(e);
                })
        },
        strs:"0123456789abcdefghijklnmopqrstuvwxyz",
        //生成随机数
        uuid: function () {
            var s = [];
            for (var i = 0; i < 36; i++) {
                s[i] =this.strs.substr(Math.floor(Math.random() * 36), 1);
            }
            var uuid = s.join("");
            return uuid;
        },
        //分享
        share: function (obj,obj2) {
            $.get("/CMS/JSToken", { url: location.href }, function (res) {
                console.log(res);
                if (res.appId) {
                    res.debug = false;
                    res.jsApiList = ["onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareWeibo","onMenuShareQQ"];
                    wx.config(res);
                }
            })
            obj.link = location.href;
            obj.success = function () {
                //分享情况
                $.post("/CMS/AddShare", { nid: obj2.Nid, cid: obj2.Cid });
            }
            wx.ready(function () {
                wx.onMenuShareTimeline(obj);
                wx.onMenuShareAppMessage(obj);
                wx.onMenuShareWeibo(obj);
                wx.onMenuShareQQ(obj);
            });
        }
    });
})(jQuery);



